package com.itheima.day10;

import com.itheima.domain.Student;
import com.itheima.util.GeneratorStudent;

import java.time.LocalDate;
import java.time.Period;
import java.util.*;

/*
1_ 第7天遗留统计
各个小组的平均年龄？
最大三个年纪的
最小三个年纪的
最大年纪的人和最小年纪的人是谁，差多久?
不同的姓各自有多少学生？
不同的年龄段有多少人？
 */
public class Test {

    public static void main(String[] args) {
        ArrayList<Student> stuList = new ArrayList<>();
        getStudents(stuList);
        //  getAgeAvg(stuList);
        //getMaxMinAge(stuList);
        //getXing(stuList);
        TreeMap<Integer, Integer> map = new TreeMap<>();
        for (Student student : stuList) {
            int age = student.getAge();
            boolean b = map.containsKey(age);
            if(b){
                Integer count = map.get(age);
                count++;
                map.put(age,count);
            }else {
                map.put(age,1);
            }
        }
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey()+"岁的人有"+entry.getValue()+"个");

        }

    }

    private static void getXing(ArrayList<Student> stuList) {
        HashMap<String, Integer> map = new HashMap<>();

        for (Student student : stuList) {
            String xing = student.getName().charAt(0)+"";
            boolean b = map.containsKey(xing);
            if (b) {
                Integer count = map.get(xing);
                map.put(xing, ++count);
            } else {
                map.put(xing, 1);

            }

        }
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey()+"的人的数量是"+entry.getValue());
        }
    }

    private static void getMaxMinAge(ArrayList<Student> stuList) {
        //最大三个年纪的
        TreeSet<Student> treeSet = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                //int result = o1.getAge() - o2.getAge();
                LocalDate birthday1 = o1.getBirthday();
                LocalDate birthday2 = o2.getBirthday();
                int result = birthday1.compareTo(birthday2);
                result = result == 0 ? o1.getSid().compareTo(o2.getSid()) : result;
                return result;
            }
        });
        for (Student student : stuList) {
            treeSet.add(student);
        }
        //
        ArrayList<Student> list = new ArrayList<>(treeSet);
        System.out.println("三个最大的");
        for (int i = 0; i < 3; i++) {
            System.out.println(list.get(list.size()-1-i));
        }
        System.out.println("三个最小的");
        for (int i = 0; i < 3; i++) {
            System.out.println(list.get(i));
        }

        Student minStudent = list.get(0);
        Student maxStudent = list.get(list.size() - 1);
        System.out.println("年龄最小的是"+minStudent);
        System.out.println("年龄最大的是"+maxStudent);

    }

    //各个小组的平均年龄？
    private static void getAgeAvg(ArrayList<Student> stuList) {

        //将所有的学生分组放入到map中 key 组名  value 组的集合
        HashMap<Integer, ArrayList<Student>> map = new HashMap<>();
        for (Student student : stuList) {
            int group = student.getGroup();
            boolean b = map.containsKey(group);
            if (b) {
                ArrayList<Student> list = map.get(group);
                list.add(student);
                map.put(group, list);
            } else {
                ArrayList<Student> list = new ArrayList<>();
                list.add(student);
                map.put(group, list);

            }

        }
        Set<Map.Entry<Integer, ArrayList<Student>>> entries = map.entrySet();
        //存放各组平均年龄的map
        HashMap<Integer, Integer> ages = new HashMap<>();
        for (Map.Entry<Integer, ArrayList<Student>> entry : entries) {
            Integer group = entry.getKey();
            ArrayList<Student> list = entry.getValue();
            int sum = 0;
            for (int i = 0; i < list.size(); i++) {
                Student student = list.get(i);
                int age = student.getAge();
                sum += age;
            }
            int avg = sum / list.size();

            ages.put(group, avg);

        }
        for (Map.Entry<Integer, Integer> entry : ages.entrySet()) {
            System.out.println("第" + entry.getKey() + "组的平均年龄是" + entry.getValue());
        }
    }


    private static void getStudents(ArrayList<Student> list) {
        int group = 0;
        for (int i = 0; i < 100; i++) {
            Student student = GeneratorStudent.getStudent();
            if (i < 10) {
                student.setSid("0000" + i);
            } else {
                student.setSid("000" + i);
            }
            group++;
            student.setGroup(group);
            if (group == 10) {
                group = 0;
            }
            list.add(student);
        }
    }

}
